<!--
  用户管理-企业用户-企业超级验证码
 -->
<template>
  <div v-if="isSuperAdmin" class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <!--  缺少姓名搜索 -->
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入企业手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
        >搜索</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleOpenSmsDialog"
      >获取验证码</el-button>
    </el-row>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column label="姓名" align="center" prop="applyUserName" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{
            moment(scope.row.createTime).format('yyy-MM-DD HH:mm:ss')
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="验证码" align="center" prop="code" />
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :title="title"
      :visible.sync="isOpenDialog"
      width="50%"
      append-to-body
    >
      <p>填写用户信息</p>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">查询登录验证码</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSuperCompanySmsCodePage,
  getSuperCompanySmsCode
} from '@/api/company-admin-api/userManage/index.js'
import { encryptDes } from '@/utils/crypto'
import { mapGetters } from 'vuex'
import moment from 'moment'
export default {
  name: 'Company',
  data() {
    const validatePhone = (rule, value, callback) => {
      const phoneReg = /^[1][0-9]{10}$/
      if (value === '') {
        return callback(new Error('手机号不能为空'))
      } else {
        if (phoneReg.test(value)) {
          return callback()
        } else {
          return callback(new Error('格式有误'))
        }
      }
    }
    return {
      moment: moment,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业验证码列表
      list: [],
      // 弹出层标题
      title: '',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        mobile: null // 手机号
      },
      form: {
        mobile: ''
      },
      // 表单校验
      rules: {
        mobile: [{ required: true, validator: validatePhone, trigger: 'blur' }]
      },
      isOpenDialog: false
    }
  },
  computed: {
    ...mapGetters(['roles']),
    isSuperAdmin() {
      return (
        Array.isArray(this.roles) && this.roles.indexOf('super_admin') > -1
      )
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleOpenSmsDialog() {
      this.isOpenDialog = true
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      // 处理查询参数
      const params = {}
      for (const key in this.queryParams) {
        if (this.queryParams[key] !== null) {
          params[key] = this.queryParams[key]
        }
      }
      if (params.phoneNumber) {
        params.phoneNumber = encryptDes(params.phoneNumber)
      }
      // 执行查询
      getSuperCompanySmsCodePage(params).then((response) => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams
      this.handleQuery()
    },
    submitForm() {
      this.$refs.form.validate((boo) => {
        if (boo) {
          getSuperCompanySmsCode({
            scene: 4,
            mobile: this.form.mobile
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message.success('操作成功')
              this.isOpenDialog = false
              this.form.mobile = ''
              this.getList()
            }
          })
        }
      })
    },
    cancel() {
      this.isOpenDialog = false
      this.form.mobile = ''
    }
  }
}
</script>
